package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.PageInfo;
import cn.itsource.pethome.basic.util.Result;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.DeparmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.org.service.IEmployeeServive;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")//swagger给生成的API做中文注释
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IEmployeeServive employeeServive;


    @PatchMapping//  相当于  @RequestMapping(value = "/department",method = RequestMethod.PATCH)
    public PageInfo<Department> findPageByQuery(@RequestBody DeparmentQuery deparmentQuery){
        return departmentService.findPageByQuery(deparmentQuery);
    }


    @GetMapping("/{id}")// 相当于      @RequestMapping(value = "/department/{id}",method = RequestMethod.GET)
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }


    /**
     * 新增和修改都走此方法
     * @param department  如果id为空，就新增，如果id不为空就是修改
     * @return
     */
    @PostMapping// 相当于    @RequestMapping(value = "/department",method = RequestMethod.POST)
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")//给对应交互风格做注解
    public Result save(@RequestBody Department department){
        try {
            departmentService.save(department);
                 return new Result();

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }

    }

    /**
     * 根据id删除数据
     * @param
     * @return
     */
    @DeleteMapping//相当于@RequestMapping(value = "/department/{id}",method = RequestMethod.DELETE)
    public Result delete(@RequestBody Department department){
        Map<String, Object> map = new HashMap<>();
        try {
            departmentService.delete(department.getId());
            return new Result("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }

    }

    /**
     * 批量删除
     * @param departments
     * @return
     */
    @DeleteMapping("/batchRemove")//相当于@RequestMapping(value = "/department/{id}",method = RequestMethod.DELETE)
    public Result batchRemove(@RequestBody List<Department> departments){
        Map<String, Object> map = new HashMap<>();
        try {
            departmentService.batchRemove(departments);
            return new Result("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }

    }

    /**
     * 查询所有员工
     * @return
     */
    @PatchMapping("/findManagers")
    public List<Employee> findmanager(){
        return employeeServive.findAll();
    }

    /**
     * 查询部门树
     * @return
     */
    @PatchMapping("/findDepartmentTree")
    public List<Department> findDepartmentTree(){
        return departmentService.findDepartmentTree();
    }

}
